package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    @Override
    public void remove(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query,UserLike.class);
    }

    @Override
    public UserLike findByIds(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        return userLike;
    }

    @Override
    public Long loveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public Long fanCount(Long likeUserId) {
        Query query = new Query(Criteria.where("likeUserId").is(likeUserId));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public Long eachLoveCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        query = new Query(Criteria.where("likeUserId").is(userId).and("userId").in(likeUserIds));
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    @Override
    public List<UserLike> eachLove(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        query = new Query(Criteria.where("likeUserId").is(userId).and("userId").in(likeUserIds));
        return mongoTemplate.find(query, UserLike.class);
    }

    @Override
    public List<UserLike> fan(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.find(query, UserLike.class);
    }

    @Override
    public List<UserLike> love(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, UserLike.class);
    }


}
